package myky.com.doctorapp.ui

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.foamtrace.photopicker.ImageCaptureManager
import com.foamtrace.photopicker.PhotoPickerActivity
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_expert_consult.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.ImageAdapter
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.entity.ImageEntity
import myky.com.doctorapp.entity.HealthRecordEntity
import myky.com.doctorapp.entity.PatientEntity
import myky.com.doctorapp.contract.ExperConsultContract
import myky.com.doctorapp.entity.LastConsultEntity
import myky.com.doctorapp.presenter.ExperConsultPresenter
import myky.com.doctorapp.utils.AddPhotoUtils
import myky.com.doctorapp.utils.Constants
import myky.com.doctorapp.utils.SystemUtil
import myky.com.doctorapp.utils.ToastUtils


/**
 * Description: 专家咨询
 * @author xiangyao
 * Created by 2019/2/25 11:01 AM
 */
class ExpertConsultActivity : BaseActivity<ExperConsultPresenter, HealthRecordEntity>(), ExperConsultContract.View, View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {

    // 相机拍照处理类
    var captureManager: ImageCaptureManager? = null
    lateinit var patientEntity: PatientEntity
    lateinit var imagePathsResult :MutableList<String>

     var doctorId: Int=-1

    private val INTENT_TYPE_CONSULT = 111

    private val INTENT_TYPE_CASE = 222

    private val USER_INFO = 333


    var chooseDissesId = -1

    var lastConsultEntity: LastConsultEntity? = null

    lateinit var imageAdapter: ImageAdapter

    private lateinit var imagelist: MutableList<ImageEntity>

    override fun onClick(p0: View?) {

        when (p0?.id) {

            R.id.activity_new_cousult_full -> {


                val content = lastConsultEntity?.content


                if (content != null) {

                    if (content.isNotEmpty()) {

                        activity_new_consult_upload_et.setText(content)

                        ToastUtils.show(resources.getString(R.string.inputSucces))

                    }
                } else {

                    ToastUtils.show(resources.getString(R.string.emptyConsultContet))
                }

            }

            R.id.ll_consult_explain->{

            }

        }
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        if (imagelist[position].path == "") {
            AddPhotoUtils.getPhotos(this, 31, 0, 9, ArrayList<String>(), 1)
        }
    }

    override fun reloadActivity() {
    }

    override fun showLastConsultContent(mLastConsultEntity: LastConsultEntity?) {


        mLastConsultEntity?.let {

            val diseaseName = it.diseaseName

            activity_new_consult_upload_nickname_text.text = diseaseName

            lastConsultEntity = it

        }


    }

    override fun injectTarget(): View = findViewById(R.id.rootLayout)

    override fun initData() {
        patientEntity = PatientEntity()
        mPresenter.findHealthRecord()
    }

    override fun initView() {
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.titleLayout)
                .init()


        val extras = intent.extras

        if(extras!=null){
        doctorId = extras.getInt(Constants.DOCTORID)

        }


        activity_new_consult_upload_nicknameIv.setOnClickListener {


            var bundle = Bundle()

            bundle.putString(Constants.DOCTORIDKEY, doctorId.toString())
            bundle.putInt(Constants.INTENT_TYPE, Constants.INTENT_TYPE_NICKNAME)


            startActivityForResult(ChooseDissesTypeActivity::class.java, bundle, INTENT_TYPE_CONSULT)


        }


        activity_new_cousult_full.setOnClickListener(this)

        ll_consult_explain.setOnClickListener(this)


        captureManager = ImageCaptureManager(this)

        imagelist = ArrayList()

        imagelist.add(ImageEntity(""))
        imageAdapter = ImageAdapter(imagelist)
        activity_consult_recycler.layoutManager = GridLayoutManager(this, 3)
        activity_consult_recycler.isNestedScrollingEnabled=false
        activity_consult_recycler.overScrollMode=View.OVER_SCROLL_NEVER
        activity_consult_recycler.adapter = imageAdapter
        imageAdapter.onItemChildClickListener = this


    }

    override fun getContenView(): Int = R.layout.activity_expert_consult

    override fun showDataSuccess(datas: HealthRecordEntity?) {
    }

    override fun showHealthRecord(perProfile: HealthRecordEntity.PerProfileBean) {


        val illnessName = perProfile.illnessNames
        if (perProfile.name != null) {
            patientEntity.name = perProfile.name
            activity_new_consult_username.text = perProfile.name
        }
        if (perProfile.sex !== -1) {
            patientEntity.sex = perProfile.sex
        }
        if (perProfile.birthday != null) patientEntity.birthday = SystemUtil.getText_Date(perProfile.birthday)
        if (perProfile.idCardNumber != null) {
            patientEntity.card = perProfile.idCardNumber
        }


    }

    override fun getPresenter(): ExperConsultPresenter {
        return ExperConsultPresenter()
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null && resultCode == RESULT_OK) {

            when (requestCode) {
                USER_INFO -> { }

                INTENT_TYPE_CONSULT -> {

                    val extras = data.extras


                    val choosseDisseName = extras.getString(Constants.DISSESNAME)


                    chooseDissesId = extras.getInt(Constants.DISSESID)

                    activity_new_consult_upload_nickname_text.text = choosseDisseName


                }

                INTENT_TYPE_CASE -> { }

                Constants.TAKE_CAMERA -> {
                    val path = data.getStringExtra("path")
                    captureManager?.galleryAddPic(path)
                    val takePhotos = java.util.ArrayList<String>()
                    imagelist.add(ImageEntity(path))
                }

                Constants.TAKE_ALBUM -> {
                    if (data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT) != null) {

                        imagePathsResult = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT)
                        for (s in imagePathsResult) {
                            imagelist.add(ImageEntity(s))


                        }

                    }
                    imageAdapter.notifyDataSetChanged()
                }

            }
        }
    }

}
